const router = require("koa-router")();
const roomModel = require("../models/room.js");
const houseModel = require("../models/house.js");
const accountModel = require("../models/account.js");
const carModel = require("../models/car.js");
const ResultHelper = require("../utils/resultHelper.js");
const VerifyHelper = require("../utils/verifyHelper.js");
const formatDateHelper = require('../utils/formatDateHelper.js')


router.get("/rooms", async ctx => {
	let {
		page,
		limit,
		name,
		house_id,
		user_id,
		id
	} = ctx.query;
	if (page == undefined) {
		page = 0
	}
	if (limit == undefined) {
		limit = 10
	}
	const query = {};

	// 构建查询条件
	if (name) {
		query.name = {
			$regex: ctx.query.name
		}; // 不区分大小写的正则表达式
	}

	if (id != undefined && id !== '0') {
		query.id = id;
	}

	if (user_id != undefined && user_id !== '0') {
		query.user_id = user_id;
	}

	if (house_id != undefined && house_id !== '0') {
		query.house_id = house_id;
	}
	console.log(query)

	var data = await roomModel.find(query).skip(Number(page - 1) * limit).limit(Number(limit));

	var total = await roomModel.find(query).countDocuments();

	//  为每个房间添加对应的 house 用户  车位 信息
	const enhancedData = await Promise.all(data.map(async room => {
		const house = await houseModel.findOne({
			id: room.house_id
		});
		console.log(house)
		const user = await accountModel.findOne({
			id: room.user_id
		});
		const car = await carModel.findOne({
			user_id: room.user_id
		});
		return {
			...room.toObject(),
			house,
			car,
			user
		};
	}));

	ctx.body = {
		code: 200,
		data: enhancedData,
		total,
		msg: "ok"
	}

})

router.post("/room", async ctx => {
	VerifyHelper.token(ctx)
	const {
		id,
		house_id,
		user_id,
		name,
		create,
		modify
	} = ctx.request.body;

	if (!name) {
		return ResultHelper.errInfo422(ctx, "请填写房间号")
	}
	if (!id) {
		// 获取house_id下的所有房间号
		const houseIdTrue = await roomModel.find({
			house_id: ctx.request.body.house_id
		});

		// 检查是否有重复的房间号
		const hasDuplicate = houseIdTrue.some(item => {
			return ctx.request.body.name === item.name;
		});

		if (hasDuplicate) {
			return ResultHelper.errInfo422(ctx, "房间号已存在");
		}
		const roomPost = new roomModel({
			id,
			house_id,
			user_id,
			name,
			create,
			modify
		});
		const res = await roomPost.save();
		ctx.body = {
			code: 200,
			msg: "添加成功"
		}
	} else {
		// 已存在数据
		// 获取下数据的内容
		const item = await roomModel.findOne({
			id: ctx.request.body.id
		});
		const res = await roomModel.updateOne({
			id: id
		}, {
			$set: {
				id,
				house_id: ctx.request.body.house_id ? ctx.request.body.house_id : item.house_id,
				user_id: ctx.request.body.user_id ? ctx.request.body.user_id : item.user_id,
				name: ctx.request.body.name ? ctx.request.body.name : item.name,
				create: ctx.request.body.create ? ctx.request.body.create : item.create,
				modify: formatDateHelper.date()
			}
		});
		ctx.body = {
			code: 200,
			msg: "编辑成功"
		}

	}
})

router.get("/del/room", async ctx => {
	VerifyHelper.token(ctx)
	const {
		id
	} = ctx.request.query;
	let res = await roomModel.deleteOne({
		id: id
	});
	ctx.body = {
		code: 200,
		msg: '删除成功'
	}
})


module.exports = router;